﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Guess
{
    public partial class GuessList : System.Web.UI.Page
    {

        protected int userID = 28;//暂时测试用户28
        protected int currentpage = 1;
        protected int pagesize = 3;
        protected string pagetype = "MyCreate";
        protected void Page_Load(object sender, EventArgs e)
        {
            string struserIDFromCookie = Common.Cookie.getCookie("userID");
            if (string.IsNullOrEmpty(struserIDFromCookie))
            {
                Response.Redirect("/");
            }
            else
            {
                userID = int.Parse(struserIDFromCookie);
            }

            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);  
            }
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                pagetype = Request.QueryString["type"];
                PageLoad();
            }
         
        }


        /// <summary>
        /// 获取竞猜列表LI
        /// </summary>
        /// <returns></returns>
        protected string getGuessListLi()
        {
            StringBuilder sbHtml = new StringBuilder();

            string strLi = Cache["guess_listli"].ToString();//竞猜列表LI
            List<int?> count;

            StringBuilder strFilter = new StringBuilder();
            switch (pagetype)
            {
                case "MyCreate":
                    strFilter.AppendFormat("FUser = {0}", userID);
                    break;
                case "MyJoin":
                    strFilter.AppendFormat("id in(select distinct FGuess from user_guess_stat where fuser ={0}) and IsCheckup=1", userID);
                    break;
                case "MyFriend":
                    strFilter.AppendFormat("FUser in(select ffriend from user_friend where fuser ={0}) and IsCheckup=1", userID);
                    break;
            }
            
            List<Jy123.Model.User_Guess_Main> guessList = new BLL.Guess.GuessList().getGuessList(strFilter.ToString(), currentpage, pagesize, "*", "OverTime", out count);

            BLL.Guess.GuessList guessListBll = new Jy123.BLL.Guess.GuessList();
            foreach (Jy123.Model.User_Guess_Main guess in guessList)
            {

                Model.User_Main userModel = new BLL.User_Main().GetModel(guess.FUser);

                string str = strLi;

                //判断竞猜状态
                if (!guess.IsSettlement && DateTime.Compare(DateTime.Now, guess.OverTime) > 0 && guess.IsCheckup)
                {
                    str = str.Replace("$GuessLiClass$", "guessGreen")  //竞猜截止CSS,IMG
                    .Replace("$GuessLiImg$", @"\images\GuClose.gif");
                    str = str.Replace("$OverTime$", guess.OverTime.ToString("MM月dd日 HH:mm"));
                    str = str.Replace("$OpenTime$", guess.OpenTime.ToString("MM月dd日 HH:mm"));
                }
                else if (DateTime.Compare(DateTime.Now, guess.OpenTime) > 0 && guess.IsSettlement && guess.IsCheckup)
                {
                    str = str.Replace("$GuessLiClass$", "guessOrange")  //已结束CSS,IMG
                    .Replace("$GuessLiImg$", @"\images\Done.gif");
                    str = str.Replace("$OverTime$", guess.OverTime.ToString("MM月dd日 HH:mm"));
                    str = str.Replace("$OpenTime$", guess.OpenTime.ToString("MM月dd日 HH:mm"));
                }
                else if (DateTime.Compare(DateTime.Now, guess.OverTime) < 0 && guess.IsCheckup)
                {
                    str = str.Replace("$GuessLiClass$", "guessBlue")    //竞猜中CSS,IMG
                    .Replace("$GuessLiImg$", @"\images\Open.gif");
                    str = str.Replace("$OverTime$", guess.OverTime.ToString("MM月dd日 HH:mm"));
                    str = str.Replace("$OpenTime$", guess.OpenTime.ToString("MM月dd日 HH:mm"));
                }
                else if (!guess.IsCheckup)
                {
                    str = str.Replace("$GuessLiClass$", "guessGray")    //未审核CSS,IMG
                    .Replace("$GuessLiImg$", @"/images/Unaudited.gif");
                    str = str.Replace("$OverTime$","");
                    str = str.Replace("$OpenTime$","");
                }


                str = str.Replace("$UserID$",userModel.ID.ToString());
                str = str.Replace("$GuessUserName$", userModel.UserName);
                str = str.Replace("$GuessUserImgUrl$", new Jy123.BLL.User.SPace().DefaultPortrait(userModel.ID));
                str = str.Replace("$GuessPoint$", guess.Point.ToString());
                str = str.Replace("$GuessMultiple$", guessListBll.getGuessMultiple(guess.Multiple));
                str = str.Replace("$GuessCount$", guessListBll.getGuessCount(guess.ID));
                str = str.Replace("$GuessTitle$", guess.Title);
                str = str.Replace("$Review$", guessListBll.getReviewCount(guess.ID));
                str = str.Replace("$GuessID$", guess.ID.ToString());

                sbHtml.Append(str);
            }

            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "GuessList.aspx?page=$num$&type=" + pagetype;
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbHtml.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            return sbHtml.ToString();
        }

        /// <summary>
        /// 加载页面
        /// </summary>
        protected void PageLoad()
        {
            Model.User_Main userModel = new BLL.User_Main().GetModel(userID);

            StringBuilder sbHtml = new StringBuilder(Cache["guess_index"].ToString());//主sb

            //公共部分
            sbHtml = sbHtml.Replace("$Javascript$", Cache["guess_js"].ToString());//JS
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$Navigation$", Cache["user_navigation_down"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧快速添加好友
            sbHtml = sbHtml.Replace("$GuessLogo$", Cache["guess_logo"].ToString());//竞猜LOGO
            sbHtml = sbHtml.Replace("$GuessMenu$", Cache["guess_menu"].ToString());//竞猜菜单

            sbHtml = sbHtml.Replace("$IndexClass$", "")   //菜单按钮选中样式
               .Replace("$AddClass$", "")
               .Replace("$TopClass$", "class=\"last\"")
               .Replace("$Cash$", Convert.ToInt32(userModel.Cash).ToString("n0"));
            switch (pagetype)
            {
                case "MyCreate":
                    sbHtml.Replace("$MyFriendClass$", "")
                    .Replace("$MyCreateClass$", "class=\"sel\"")
                    .Replace("$MyJoinClass$", "");
                    break;
                case "MyJoin":
                    sbHtml.Replace("$MyFriendClass$", "")
                   .Replace("$MyCreateClass$", "")
                   .Replace("$MyJoinClass$", "class=\"sel\"");
                    break;
                case "MyFriend":
                    sbHtml.Replace("$MyFriendClass$", "class=\"sel\"")
                   .Replace("$MyCreateClass$", "")
                   .Replace("$MyJoinClass$", "");
                    break;
            }
            sbHtml = sbHtml.Replace("$Content$", Cache["guess_list"].ToString());//竞猜列表

            sbHtml = sbHtml.Replace("$GuessListLi$", getGuessListLi());//竞猜列表LI


            Response.Write(sbHtml.ToString());

        }


    }

}